#!/usr/bin/env python
import os, sys, glob

os.chdir("po")
os.system("intltool-update --pot --gettext-package=messages --verbose")

command = None
option = None
try:
    command = sys.argv[1]
    option = sys.argv[2]
except IndexError:
    pass

if "update" == command and not option is None:
    # merge messages.pot with option.pot
    os.system("msgmerge -U %s.po messages.pot" % option)
    
if "compile" == command:
    # compile po-files
    files = glob.glob("*.po")
    
    for f in files:
        l = os.path.splitext(f)
        os.system('mkdir -p -m 0777 %s/LC_MESSAGES' % l[0])

        print "Generating translation for %s locale" % l[0]
        os.system('msgmerge -o - %s messages.pot | msgfmt -c -o %s/LC_MESSAGES/pybookshelf.mo -' % (f, l[0]))
    
else:
    pass

os.chdir("..")
